import { ref } from 'vue'
import store from '@/store'
import { defineStore } from 'pinia'
import {
  getToken,
  removeToken,
  setToken,
  getUserName,
  removeUserName,
  getPhone,
  removePhone,
  setLevel,
  setAdcode,
  removeAdcode,
  removeLevel,
  getAdcode,
  getLevel,
  getAreaName,
  setAreaName,
  removeAreaName,
} from '@/utils/cache/cookies'
import { loginForm } from '@/api/login'
import { type LoginSMSRequestData } from '@/api/login/types/login'

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref<string>(getToken() || '')
    const realname = ref<string>(getUserName() || '')
    const realphone = ref<string>(getPhone() || '')
    const adcode = ref<string>(getAdcode() || '10000')
    const level = ref<string>(getLevel() || 'country')
    const areaname = ref<string>(getAreaName() || '中国')
    const type = ref<string>('电柜总数')
    const deepTree = ref<any[]>([])
    /** 登录 */
    const login = async (data: LoginSMSRequestData) => {
      const res = await loginForm({
        type: 'SMS',
        channel: 'Service',
        contextData: {
          phoneNumber: data.phoneNumber,
          code: data.code,
        },
      })
      setToken(res.resultData)
      setLevel(res.level)
      setAdcode(res.adcode)
      setAreaName(res.areaName)
      areaname.value = res.areaName
      adcode.value = res.adcode
      level.value = res.level
    }

    /** 微信登录 */
    const wxLogin = async (openId: string) => {
      const res = await loginForm({
        type: 'WeChat',
        channel: 'Service',
        contextData: {
          code: openId,
        },
      })
      setToken(res.token)
      setLevel(res.level)
      setAdcode(res.adcode)
      setAreaName(res.areaName)
      areaname.value = res.areaName
      adcode.value = res.adcode
      level.value = res.level
    }

    /** 登出 */
    const logout = async () => {
      removeToken()
      removePhone()
      removeUserName()
      removeAdcode()
      removeLevel()
      removeAreaName()
      token.value = ''
      realname.value = ''
      realphone.value = ''
      adcode.value = ''
      level.value = ''
      areaname.value = ''
    }
    /** 重置 Token */
    const resetToken = () => {
      removeToken()
      token.value = ''
    }
    const setType = (data: string) => {
      type.value = data
    }
    const getType = () => {
      return type.value
    }
    const setMapAdcode = (c: string) => {
      adcode.value = c
    }
    const getMapAdcode = () => {
      return adcode.value
    }
    const setMapLevel = (c: string) => {
      level.value = c
    }
    const getMapLevel = () => {
      return level.value
    }
    const setMapAreaName = (c: string) => {
      areaname.value = c
    }
    const getMapAreaName = () => {
      return areaname.value
    }
    const getMapTree = () => {
      return deepTree.value
    }
    const popMapTree = () => {
      deepTree.value.pop()
    }
    const pushMapTree = (c: any, n: string) => {
      if (deepTree.value.findIndex((item) => item.areaName === n) !== -1) {
        return
      }
      deepTree.value.push(c)
    }
    const removeMapTree = () => {
      deepTree.value = []
    }
    return {
      token,
      realname,
      realphone,
      login,
      wxLogin,
      logout,
      resetToken,
      setType,
      getType,
      setMapAdcode,
      getMapAdcode,
      setMapLevel,
      getMapLevel,
      setMapAreaName,
      getMapAreaName,
      pushMapTree,
      getMapTree,
      popMapTree,
      removeMapTree,
    }
  },
  {
    persist: true,
  },
)

/** 在 setup 外使用 */
export function useUserStoreHook() {
  return useUserStore(store)
}
